package com.qixiaobao.web.controller.nondirect.invoice;

import com.qixiaobao.common.annotation.Log;
import com.qixiaobao.common.annotation.RepeatSubmit;
import com.qixiaobao.common.core.domain.AjaxResult;
import com.qixiaobao.common.core.page.TableDataInfo;
import com.qixiaobao.common.enums.BusinessType;
import com.qixiaobao.common.exception.UtilException;
import com.qixiaobao.common.utils.DateUtils;
import com.qixiaobao.common.utils.StringUtils;
import com.qixiaobao.common.utils.easyExcel.EasyExcelUtil;
import com.qixiaobao.nondirect.invoice.domain.InvoiceStatistic;
import com.qixiaobao.nondirect.invoice.domain.InvoiceStatisticNew;
import com.qixiaobao.nondirect.invoice.domain.dto.NonDirectInvoiceDTO;
import com.qixiaobao.nondirect.invoice.domain.dto.NonDirectInvoiceStatisticDTO;
import com.qixiaobao.nondirect.invoice.domain.vo.NonDirectWaitInvoiceChargeVO;
import com.qixiaobao.nondirect.invoice.domain.vo.NonDirectWaitInvoiceVO;
import com.qixiaobao.nondirect.invoice.service.IInvoiceStatisticService;
import com.qixiaobao.nondirect.invoice.service.INonDirectWaitInvoiceService;
import com.qixiaobao.nondirect.invoice.domain.Bill;
import com.qixiaobao.system.domain.dto.ChargeDTO;
import com.qixiaobao.nondirect.invoice.service.IBillService;
import com.qixiaobao.web.controller.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description 非直投发票管理
 * @Author yangxm
 * @Date 2025/7/4 13:23
 */
@RequestMapping("/nonDirect/invoice")
@RestController
public class NonDirectInvoiceController extends BaseController {

    @Autowired
    private INonDirectWaitInvoiceService nonDirectWaitInvoiceService;
    @Autowired
    private IBillService billService;
    @Autowired
    private IInvoiceStatisticService invoiceStatisticService;

    /**
     * 待开票列表
     * @param nonDirectInvoiceDTO
     * @return
     */
    @RequestMapping("/wait/list")
    public TableDataInfo waitList(NonDirectInvoiceDTO nonDirectInvoiceDTO){
        nonDirectInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        startPage();
        return getDataTable(nonDirectWaitInvoiceService.selectWaitInvoiceList(nonDirectInvoiceDTO));
    }

    /**
     * @Description 待开票列表 - 开票金额
     * @Author yangxm
     * @Date 2025/7/4 14:42
     * @param nonDirectInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RequestMapping("/wait/total")
    public AjaxResult waitTotal(NonDirectInvoiceDTO nonDirectInvoiceDTO){
        nonDirectInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        return success(nonDirectWaitInvoiceService.selectWaitInvoiceTotal(nonDirectInvoiceDTO));
    }

    /**
     * 待开票列表 - 导出
     * @param nonDirectInvoiceDTO
     * @return
     */
    @Log(title = "非直投发票管理-待开票", businessType = BusinessType.EXPORT)
    @RequestMapping("/wait/export")
    public void waitExport(NonDirectInvoiceDTO nonDirectInvoiceDTO, HttpServletResponse response){
        nonDirectInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        List<NonDirectWaitInvoiceVO> list = nonDirectWaitInvoiceService.selectWaitInvoiceList(nonDirectInvoiceDTO);
        try {
            EasyExcelUtil.writeExcel(response, list, NonDirectWaitInvoiceVO.class, "非直投发票管理-待开票", "sheet1");
        } catch (IOException e) {
            throw new UtilException("导出失败：" + e.getMessage());
        }
    }

    /**
     * 待开票列表 - 待开票详情
     * @param chargeDTO
     * @return
     */
    @RequestMapping("/wait/charge/list")
    public TableDataInfo waitChargeList(ChargeDTO chargeDTO){
        startPage();
        return getDataTable(nonDirectWaitInvoiceService.selectInsureFeeChargeList(chargeDTO));
    }

    /**
     * 待开票列表 - 待开票详情 - 导出
     * @param chargeDTO
     * @return
     */
    @Log(title = "非直投发票管理-待开票", businessType = BusinessType.EXPORT, businessKey = "详情")
    @RequestMapping("/wait/charge/export")
    public void waitChargeExport(ChargeDTO chargeDTO, HttpServletResponse response){
        List<NonDirectWaitInvoiceChargeVO> list = nonDirectWaitInvoiceService.selectInsureFeeChargeList(chargeDTO);
        try {
            EasyExcelUtil.writeExcel(response, list, NonDirectWaitInvoiceChargeVO.class, "非直投发票管理-待开票详情", "sheet1");
        } catch (IOException e) {
            throw new UtilException("导出失败：" + e.getMessage());
        }
    }

    /**
     * @Description 待开票 - 开票操作
     * @Author yangxm
     * @Date 2025/7/4 16:18
     * @param bill
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "非直投发票管理-待开票", businessType = BusinessType.UPDATE, businessKey = "开票")
    @PostMapping("/wait/makeBill")
    public AjaxResult makeBill(@RequestBody Bill bill){
        return toAjax(billService.makeInvoiceBill(bill));
    }

    /**
     * @Description 开票中列表
     * @Author yangxm
     * @Date 2025/7/4 16:38
     * @param nonDirectInvoiceDTO
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/invoicing/list")
    public TableDataInfo invoicingList(NonDirectInvoiceDTO nonDirectInvoiceDTO){
        nonDirectInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        startPage();
        return getDataTable(billService.selectInvoicingList(nonDirectInvoiceDTO));
    }

    /**
     * @Description 开票中列表 - 开票金额
     * @Author yangxm
     * @Date 2025/7/4 16:39
     * @param nonDirectInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/invoicing/total")
    public AjaxResult invoicingTotal(NonDirectInvoiceDTO nonDirectInvoiceDTO){
        nonDirectInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        return success(billService.selectInvoicingTotal(nonDirectInvoiceDTO));
    }

    /**
     * @Description 开票中列表 - 导出
     * @Author yangxm
     * @Date 2025/7/4 16:40
     * @param nonDirectInvoiceDTO
     * @return void
     */
    @Log(title = "非直投发票管理-开票中", businessType = BusinessType.EXPORT)
    @GetMapping("/invoicing/export")
    public void invoicingExport(NonDirectInvoiceDTO nonDirectInvoiceDTO, HttpServletResponse response){
        nonDirectInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        billService.invoicingExport(nonDirectInvoiceDTO, response);
    }

    /**
     * @Description 开票中 - 上传发票
     * @Author yangxm
     * @Date 2025/7/7 9:38
     * @param nonDirectInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "非直投发票管理-开票中", businessType = BusinessType.UPLOAD, businessKey = "上传发票")
    @PostMapping("/invoicing/uploadInvoiceFile")
    public AjaxResult uploadInvoicingFile(@RequestBody NonDirectInvoiceDTO nonDirectInvoiceDTO){
        return toAjax(billService.uploadInvoicingFile(nonDirectInvoiceDTO));
    }

    /**
     * @Description 开票中 - 合并开票
     * @Author yangxm
     * @Date 2025/7/7 11:12
     * @param nonDirectInvoiceDTO 
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "非直投发票管理-开票中", businessType = BusinessType.UPLOAD, businessKey = "合并开票")
    @PostMapping("/invoicing/uploadMergeInvoice")
    public AjaxResult uploadMergeInvoiceFile(NonDirectInvoiceDTO nonDirectInvoiceDTO){
        return toAjax(billService.uploadMergeInvoiceFile(nonDirectInvoiceDTO));
    }

    /**
     * @Description 待审核、已开票 列表
     * @Author yangxm
     * @Date 2025/7/7 14:27
     * @param nonDirectInvoiceDTO
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/list")
    public TableDataInfo list(NonDirectInvoiceDTO nonDirectInvoiceDTO){
        nonDirectInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        startPage();
        return getDataTable(billService.selectInvoiceList(nonDirectInvoiceDTO));
    }

    /**
     * @Description 待审核、已开票 - 列表 - 应开票金额、实际开票金额总计
     * @Author yangxm
     * @Date 2025/7/7 15:36
     * @param nonDirectInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/total")
    public AjaxResult total(NonDirectInvoiceDTO nonDirectInvoiceDTO){
        nonDirectInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        return success(billService.selectInvoiceTotal(nonDirectInvoiceDTO));
    }

    /**
     * @Description 待审核、已开票 - 导出
     * @Author yangxm
     * @Date 2025/7/7 15:45
     * @param nonDirectInvoiceDTO
     * @param response
     */
    @Log(title = "非直投发票管理-待审核、已开票", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(NonDirectInvoiceDTO nonDirectInvoiceDTO, HttpServletResponse response){
        nonDirectInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        billService.invoiceExport(nonDirectInvoiceDTO, response);
    }

    /**
     * @Description 待审核、已开票 - 合并发票 - 开票详情
     * @Author yangxm
     * @Date 2025/7/7 15:54
     * @param nonDirectInvoiceDTO
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/merge/detail")
    public TableDataInfo billMergeDetailList(NonDirectInvoiceDTO nonDirectInvoiceDTO){
        startPage();
        return getDataTable(billService.selectBillMergeDetailList(nonDirectInvoiceDTO));
    }

    /**
     * @Description 待审核、已开票 - 发票文件
     * @Author yangxm
     * @Date 2025/7/7 16:02
     * @param nonDirectInvoiceDTO 
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/invoiceFile")
    public AjaxResult invoiceFile(NonDirectInvoiceDTO nonDirectInvoiceDTO){
        return success(billService.selectInvoiceFileList(nonDirectInvoiceDTO));
    }

    /**
     * @Description 待审核、已开票 - 发票 - 一键下载
     * @Author yangxm
     * @Date 2025/7/7 16:54
     * @param nonDirectInvoiceDTO
     * @param response
     */
    @GetMapping("/invoiceFile/download")
    public void invoiceFileDownload(NonDirectInvoiceDTO nonDirectInvoiceDTO, HttpServletResponse response){
        billService.invoiceFileDownload(nonDirectInvoiceDTO, response);
    }

    /**
     * @Description 待审核 - 重新上传发票文件
     * @Author yangxm
     * @Date 2025/7/7 16:02
     * @param nonDirectInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "非直投发票管理-待审核", businessType = BusinessType.UPLOAD, businessKey = "重新上传发票")
    @PostMapping("/reUploadInvoiceFile")
    public AjaxResult reUploadInvoiceFile(@RequestBody NonDirectInvoiceDTO nonDirectInvoiceDTO){
        return toAjax(billService.reUploadInvoiceFile(nonDirectInvoiceDTO));
    }


    /**
     * @Description 已开票 - 根据筛选条件，下载发票文件，供应商简称必选
     * @Author yangxm
     * @Date 2025/7/7 16:38
     * @param nonDirectInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/downloadInvoiceFile")
    public AjaxResult downloadInvoiceFile(NonDirectInvoiceDTO nonDirectInvoiceDTO){
        billService.downloadInvoiceFile(nonDirectInvoiceDTO);
        return AjaxResult.success();
    }


    /**
     * @Description 开票中、待审核、已开票 修改备注
     * @Author yangxm
     * @Date 2025/7/4 16:41
     * @param nonDirectInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @Log(title = "非直投发票管理-开票中、待审核、已开票", businessType = BusinessType.UPDATE, businessKey = "修改备注")
    @PostMapping("/updateRemark")
    public AjaxResult updateRemark(NonDirectInvoiceDTO nonDirectInvoiceDTO){
        return toAjax(billService.updateRemark(nonDirectInvoiceDTO));
    }

    /**
     * @Description 开票中、待审核、已开票 - 费用详情
     * @Author yangxm
     * @Date 2025/7/7 13:50
     * @param chargeDTO
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/charge/list")
    public TableDataInfo chargeList(ChargeDTO chargeDTO){
        if (chargeDTO.getBillId() == null){
            throw new UtilException("参数错误");
        }
        startPage();
        return getDataTable(billService.selectChargeList(chargeDTO));
    }

    /**
     * @Description 发票汇总 列表
     * @Author yangxm
     * @Date 2025/7/8 10:43
     * @param nonDirectInvoiceStatisticDTO
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/statistic/list")
    public TableDataInfo statisticList (NonDirectInvoiceStatisticDTO nonDirectInvoiceStatisticDTO){
        if (StringUtils.isBlank(nonDirectInvoiceStatisticDTO.getYearMonth())){
            throw new UtilException("参数：汇总月份不能为空！");
        }
        startPage();
        return getDataTable(invoiceStatisticService.selectInvoiceStatisticList(nonDirectInvoiceStatisticDTO));
    }


    /**
     * @Description 发票汇总 列表 - 导出
     * @Author yangxm
     * @Date 2025/7/8 11:21
     * @param nonDirectInvoiceStatisticDTO
     */
    @Log(title = "非直投发票管理-发票汇总", businessType = BusinessType.EXPORT)
    @GetMapping("/statistic/export")
    public void statisticExport(NonDirectInvoiceStatisticDTO nonDirectInvoiceStatisticDTO, HttpServletResponse response){
        if (StringUtils.isBlank(nonDirectInvoiceStatisticDTO.getYearMonth())){
            throw new UtilException("参数：汇总月份不能为空！");
        }
        List<InvoiceStatistic> list = invoiceStatisticService.selectInvoiceStatisticList(nonDirectInvoiceStatisticDTO);
        String fileName = "非直投发票汇总";
        try {
            EasyExcelUtil.writeExcel(response, list, InvoiceStatistic.class, fileName, "sheet1");
        } catch (IOException e) {
            throw new UtilException("非直投发票汇总导出失败：" + e.getMessage());
        }
    }

    /**
     * @Description 发票汇总(新) 列表
     * @Author yangxm
     * @Date 2025/7/8 11:42
     * @param nonDirectInvoiceStatisticDTO
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/statistic/new/list")
    public TableDataInfo statisticNewList(NonDirectInvoiceStatisticDTO nonDirectInvoiceStatisticDTO){
        if (StringUtils.isBlank(nonDirectInvoiceStatisticDTO.getYearMonth())){
            throw new IllegalArgumentException("参数：汇总月份不能为空！");
        }
        startPage();
        return getDataTable(invoiceStatisticService.selectInvoiceStatisticNewList(nonDirectInvoiceStatisticDTO));
    }


    /**
     * @Description 发票汇总(新) 列表 - 导出
     * @Author yangxm
     * @Date 2025/7/8 13:35
     * @param nonDirectInvoiceStatisticDTO
     * @param response
     */
    @Log(title = "非直投发票管理-发票汇总(新)", businessType = BusinessType.EXPORT)
    @GetMapping("/statistic/new/export")
    public void statisticNewExport(NonDirectInvoiceStatisticDTO nonDirectInvoiceStatisticDTO, HttpServletResponse response){
        if (StringUtils.isBlank(nonDirectInvoiceStatisticDTO.getYearMonth())){
            throw new IllegalArgumentException("参数：汇总月份不能为空！");
        }
        List<InvoiceStatisticNew> list = invoiceStatisticService.selectInvoiceStatisticNewList(nonDirectInvoiceStatisticDTO);
        if (StringUtils.isNotEmpty(list)){
            InvoiceStatisticNew item;
            for (int i = 0; i < list.size(); i++) {
                item = list.get(i);
                item.setIndexNo(i + 1);
            }
        }
        String fileName = "非直投发票汇总(新)";
        try {
            EasyExcelUtil.writeExcel(response, list, InvoiceStatisticNew.class, fileName, "sheet1");
        } catch (IOException e) {
            throw new UtilException("非直投发票汇总(新)导出失败：" + e.getMessage());
        }
    }

    /**
     * @Description 发票汇总（新），各数据统计
     * @Author yangxm
     * @Date 2025/7/8 13:37
     * @param nonDirectInvoiceStatisticDTO
     */
    @GetMapping("/statistic/new/total")
    public AjaxResult newTotal(NonDirectInvoiceStatisticDTO nonDirectInvoiceStatisticDTO){
        return AjaxResult.success(invoiceStatisticService.newTotal(nonDirectInvoiceStatisticDTO));
    }
}
